using UnityEngine;
using System.Collections;

public class csBoomCtrl : csBulletAttributes 
{
#region Definition
	protected enum STATE_TYPE
	{
		None = 0,		//None
		Dropping,		//Dropping
		Explosion,		//Explosion
	}
	
	protected delegate void STATE_FUNC();
#endregion
	
#region InputData
	public Vector3		DropDirect				= Vector3.forward;	//Drop direction
	public float		InitialExplosionRange	= 90.0f;			//Initial explosion range.
	public float		FallSpeed				= 0.0f;				//Falling Speed after dropped.	Unit: Pixel per Frame.
	public int			ExplosionDelayTime		= 60;				//Explosion delay time.			Unit: Frame.
	public float		ExplosionRange			= 100.0f;			//Radius of explosion region.	Unit: Pixel.
	public int			ExplosionTime			= 120;				//The explosion time.			Unit: Frame.
	public GameObject	ExplosionFXPrefab		= null;				//Prefab for explosion FX.
#endregion
	
#region DataMember
//------ Attributes------
	protected Transform		mTransfom 			= null;			//Declare for performance.
	protected GameObject 	mExplosionFX		= null;			//Explosion FX.
	protected float			mExplosionSpreadLen = 0.0f;			//Explosion Spread Len.			Unit:Pixel per Frame.
	
//------- Counter -------
	protected int mExplosionDelayCounter	= 0;				//Counter for explosion delay time
	protected int mExplosionTimeCounter		= 0;				//Counter for explosion time
	
//------- State ---------
	protected STATE_TYPE mCurState = csBoomCtrl.STATE_TYPE.None;
	protected System.Collections.Generic.Dictionary<STATE_TYPE, STATE_FUNC> mStateFuncList = new System.Collections.Generic.Dictionary<STATE_TYPE, STATE_FUNC>();
#endregion
	
#region MemberFunc
//Interface for csBoomDropper	
	public void DropBoom()
	{
		this.mCurState = csBoomCtrl.STATE_TYPE.Dropping;
	}
#endregion
	
#region EventFunc
	public void OnDestroySelf()
	{
		if(this.mExplosionFX)
			Object.Destroy(this.mExplosionFX);
			
		Object.Destroy(this.gameObject);
	}
#endregion

#region StateFunc
	protected void State_Dropping()
	{
		if(this.mExplosionDelayCounter++ >= this.ExplosionDelayTime) 
		{
			this.mExplosionSpreadLen = this.ExplosionRange / (float)this.ExplosionTime;
			
		//Generate the Explosion FX
			this.mExplosionFX = Object.Instantiate(this.ExplosionFXPrefab, this.mTransfom.position, Quaternion.identity) as GameObject;
			
		//Enable collider
			this.gameObject.collider.enabled = true;
			
		//Run Explosion State
			this.mCurState = csBoomCtrl.STATE_TYPE.Explosion;
			return;
		}
		
	//Falling
		this.mTransfom.position += this.DropDirect * this.FallSpeed;
	}
	
	protected void State_Explosion()
	{
		if(this.mExplosionTimeCounter++ >= this.ExplosionTime)
		{
			this.mCurState = csBoomCtrl.STATE_TYPE.None;
			
			this.OnDestroySelf();
			return;
		}
		
	//Spread
		this.mTransfom.localScale = new Vector3((this.mTransfom.localScale.x + this.mExplosionSpreadLen), 
		                                        (this.mTransfom.localScale.y + this.mExplosionSpreadLen),
		                                        this.mTransfom.localScale.z);
	}
#endregion
	
#region SystemFunc
	void Awake()
	{
	//Build State List
		this.mStateFuncList[STATE_TYPE.None]		= null;
		this.mStateFuncList[STATE_TYPE.Dropping]	= new csBoomCtrl.STATE_FUNC(this.State_Dropping);
		this.mStateFuncList[STATE_TYPE.Explosion]	= new csBoomCtrl.STATE_FUNC(this.State_Explosion);
		
	//Set reference
		this.mTransfom = this.transform;
		
	//Set Scale
		this.mTransfom.localScale = new Vector3(this.InitialExplosionRange, this.InitialExplosionRange, 0.0f);
	}
	
	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(this.mStateFuncList[this.mCurState] != null)
			this.mStateFuncList[this.mCurState]();
	}	
#endregion
	
#region PropertiesOP
#endregion
}
